<?php
/**
 * Created by PhpStorm.
 * User: tangmusen
 * Date: 2017/10/30
 * Time: 11:08
 */

namespace app\api\controller;



use think\Cache;
use think\Config;
use think\Controller;
use think\Db;
use think\Request;

class Group extends Controller
{

    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        parent::__construct();
    }

    /**
     * 创建群组
     */
    public function create_group(Request $request){
        $uid        = $request->post('uid');
        $group_name = $request->post('group_name');
        if(!$uid || !$group_name){
            json_return(204,'缺少参数');
        }
        $count = Db::name('groups')->where('uid',$uid)->count();
        if($count>5){
            json_return(201,'已达到建群上线');
        }
        $insert_data = [
            'uid'=>$uid,
            'group_name' =>$group_name
        ];
        $group_id = Db::name('groups')->insertGetId($insert_data);
        add_group($uid,$group_id,1);
        $member_ids_arr   = [];
        $member_ids_arr[] = $uid;
        $RongYun = new Rongyun();
        $RongYun->create_group($member_ids_arr,$group_id,$group_name);
        $data    = $insert_data;
        $data['group_id'] = $group_id;
        if($group_id){
            json_return(200, '创建成功', $data);
        }else{
            json_return(201, '创建失败');
        }
    }

    /**
     * 修改群组名称
     */
    public function modify_group_name(Request $request){

        $uid        = $request->post('uid');
        $group_id   = $request->post('group_id');
        $group_name = $request->post('group_name');
        if(!$uid || !$group_id || !$group_name){
            json_return(204,'缺少参数');
        }
        $maps0 = [
            'uid'=>$uid,
            'id' =>$group_id
        ];
        $group_info = Db::name('groups')->where($maps0)->find();
        if(!$group_info){
            json_return(201,'非群管理员不能修改群昵称');
        }
        $ups = [
            'group_name'=>$group_name
        ];
        $map_up = [
            'id' =>$group_id
        ];
        $result0 = Db::name('groups')->where($map_up)->update($ups);
        $RongYun = new Rongyun();
        $RongYun-> refresh_group($group_id,$group_name);
        if($result0){
            json_return(200, '修改成功');
        }else{
            json_return(201, '修改失败');
        }

    }

    /**
     * 将用户拉入群组
     */
    public function push_to_group(Request $request){

        $uid        = $request->post('uid');
        $group_id   = $request->post('group_id');
        $member_ids = $request->post('member_ids');
        if(!$uid || !$member_ids || !$member_ids){
            json_return(204,'缺少参数');
        }
        $member_ids_arr = json_decode($member_ids,true);
        if(!is_array($member_ids_arr)){
            json_return(201,'参数格式有误');
        }
        $map_up = [
            'uid'=>$uid,
            'id' =>$group_id
        ];
        $group_info     = Db::name('groups')->where($map_up)->find();
        if(!$group_info){
            json_return(201,'非群主不能拉人');
        }
        $group_name = $group_info['group_name'];
        foreach ($member_ids_arr as $k=>$v){
            add_group($v,$group_id);
        }
        $RongYun = new Rongyun();
        $RongYun-> join_group($member_ids_arr,$group_id,$group_name);
        $group_list = Db::name('groups_ship')->select();
        json_return(200, '添加成功',$group_list);
    }

    /**
     * 移除群组
     */
    public function quit_to_group(Request $request){
        $uid        = $request->post('uid');
        $group_id   = $request->post('group_id');
        $member_ids = $request->post('member_ids');
        if(!$uid || !$group_id || !$member_ids) {
            json_return(204, '缺少参数');
        }
        $member_ids_arr = json_decode($member_ids,true);
        if(!is_array($member_ids_arr)){
            json_return(201,'参数格式有误');
        }
        $map_up = [
            'uid'=>$uid,
            'id' =>$group_id
        ];
        $group_info     = Db::name('groups')->where($map_up)->find();
        if(!$group_info){
            json_return(201,'未找到群组信息');
        }
        $maps0 = [
            'uid'=>['in',$member_ids_arr]
        ];
        $result0 = Db::name('groups_ship')->where($maps0)->delete();
        $RongYun = new Rongyun();
        $RongYun-> quit_group($member_ids_arr,$group_id);
        $group_list = Db::name('groups_ship')->select();
        if($result0){
            json_return(200, '移除成功',$group_list);
        }else{
            json_return(201, '移除失败');
        }
    }

    /**
     * 退出群组
     */
    public function abort_to_group(Request $request){
        $uid        = $request->post('uid');
        $group_id   = $request->post('group_id');
        if(!$uid || !$group_id ) {
            json_return(204, '缺少参数');
        }
        $member_ids_arr = [$uid];
        $maps0 = [
            'uid'     =>$uid,
            'group_id'=>$group_id
        ];
         Db::name('groups_ship')->where($maps0)->delete();
        $RongYun = new Rongyun();
        $RongYun-> quit_group($member_ids_arr,$group_id);

        json_return(200, '退出成功');

    }

    /**
     * 我的群组
     */
    public function get_my_groups(Request $request){
        $uid        = $request->post('uid');
        if(!$uid) {
            json_return(204, '缺少参数');
        }
        $group_ids = Db::name('groups_ship')->where('uid',$uid)->value('group_id');
        if(!$group_ids){
            json_return(201, '暂未加入任何群组');
        }
        $data = Db::name('groups')->where('id','in',$group_ids)->select();
        if($data){
            json_return(200, '获取群组成功',$data);
        }else{
            json_return(201, '获取群组失败');
        }
    }

    /**
     * 获取谋个群组的成员信息
     */
    public function get_group_member_info(Request $request){
        $uid        = $request->post('uid');
        $group_id   = $request->post('group_id');
        if(!$uid ||!$group_id) {
            json_return(204, '缺少参数');
        }
        $map = [
            'g.id'=>$group_id
        ];
        $data = Db::name('groups_ship')
            ->alias('g')
            ->field('m.id,m.username,m.mobile,m.nickname,m.head,g.is_owner')
            ->join('dl_member m','m.id=g.uid')
            ->where($map)
            ->select();
        foreach ($data as $kk=>$vv){
            $data[$kk]['head'] = Config::get('img_url').$vv['head'];
        }
        if($data){
            json_return(200, '获取成员信息成功',$data);
        }else{
            json_return(201, '获取成员信息失败');
        }

    }

    /**
     * 解散群组
     */
    public function do_dismiss_group(Request $request){
        $uid        = $request->post('uid');
        $group_id   = $request->post('group_id');
        if(!$uid || !$group_id) {
            json_return(204, '缺少参数');
        }
        $map_up = [
            'uid'=>$uid,
            'id' =>$group_id
        ];
        $group_info     = Db::name('groups')->where($map_up)->find();
        if(!$group_info){
            json_return(201,'未找到群组信息');
        }
        $result0 = Db::name('groups')->where($map_up)->delete();
        $RongYun = new Rongyun();
        $RongYun-> dismiss_group($uid,$group_id);
        if($result0){
            json_return(200, '解散成功');
        }else{
            json_return(201, '解散失败');
        }
    }

    /**
     * 根据id查询群信息
     */
    public function get_group_info(Request $request){
        $uid        = $request->post('uid');
        $group_id   = $request->post('group_id');
        if(!$uid || !$group_id) {
            json_return(204, '缺少参数');
        }
        $group_info = Db::name('groups')->where('id',$group_id)->find();
        $member_ids = Db::name('groups_ship')->where('group_id',$group_id)->column('uid');
        $field = "id,username,mobile,nickname,head";
        $data  = Db::name('member')->field($field)->where('id','in',$member_ids)->select();
        foreach ($data as $kk=>$vv){
            $data[$kk]['head'] = Config::get('img_url').$vv['head'];
            if($vv['id']==$uid){
                $data[$kk]['is_owner'] = 1;
            }else{
                $data[$kk]['is_owner'] = 0;
            }
        }
        $group_info['member_info'] = $data;
        if($group_info){
            json_return(200, '成功',$group_info);
        }else{
            json_return(201, '失败');
        }

    }

    /**
     * 添加好友
     */
    public function do_add_friend(Request $request){
        $uid       = $request->post('uid');
        $friend_id = $request->post('friend_id');
        if(!$uid|| !$friend_id) {
            json_return(204, '缺少参数');
        }
        $maps = [
            'uid'      =>$uid,
            'friend_id'=>$friend_id
        ];
        $be_groups =  Db::name('friends_ship')->where($maps)->find();
        if($be_groups){
                json_return(201, '已是好友');
        }
        $insert = [
            'uid'      =>$uid,
            'friend_id'=>$friend_id
        ];
        $result0 =  Db::name('friends_ship')->insert($insert);
        if($result0){
            json_return(200, '添加好友成功');
        }else{
            json_return(201, '添加好友失败');
        }



    }

    /**
     * 获取好友列表
     */
    public function get_friend_list(Request $request){
        $uid        = $request->post('uid');
        $friends_id = Db::name('friends_ship')->where('uid',$uid)->column('friend_id');
        if(!$friends_id){
            json_return(201, '暂无好友');
        }
        $map = [
            'id'=>['in',$friends_id]
        ];
        $field = "id,username,mobile,nickname,head";
        $data = Db::name('member')->field($field)->where($map)->select();
        foreach ($data as $kk=>$vv){
            $data[$kk]['head'] = Config::get('img_url').$vv['head'];
        }
        json_return(200, '获取好友成功',$data);
    }

    /**
     * 删除好友
     */
    public function remove_friend(Request $request){
        $uid       = $request->post('uid');
        $friend_id = $request->post('friend_id');
        if(!$uid || !$friend_id) {
            json_return(204, '缺少参数');
        }
        $maps = [
            'uid'      =>$uid,
            'friend_id'=>$friend_id
        ];
        Db::name('friends_ship')->where($maps)->delete();
        json_return(200, '删除好友成功');
    }

    /**
     * 获取好友的信息
     */
    public function get_friend_info(Request $request){
        $uid       = $request->post('uid');
        $friend_id = $request->post('friend_id');
        $mobile    = $request->post('mobile');
        if(!$uid) {
            json_return(204, '缺少参数');
        }
        if($friend_id){
            $w['id']     = $friend_id;
        }
        if($mobile){
            $w['mobile'] = $mobile;
        }
        $field = "id,username,mobile,nickname,head";
        $data = Db::name('member')->field($field)->where($w)->find();
        if(!$data){
            json_return(201, '获取好友失败');
        }else {
            $data['head'] = Config::get('img_url') . $data['head'];
            json_return(200, '获取好友成功',$data);
        }
    }

}